package com.example.app2;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app2.pojo.GoodsInfo;

import java.util.List;

public class ShoppingMallActivity extends AppCompatActivity {
    private ShoppingDBHelper mDBHelper;
    private GridLayout gl_channel;
    private TextView tv_count;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_mall);
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("手机商场");
        findViewById(R.id.iv_back).setVisibility(View.INVISIBLE);
        findViewById(R.id.iv_cart).setOnClickListener(v->{
            if(v.getId()==R.id.iv_cart)
            {
                Intent intent = new Intent(ShoppingMallActivity.this, ShoppingCartActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        tv_count = findViewById(R.id.tv_count);
        gl_channel = findViewById(R.id.gl_channel);
        mDBHelper=ShoppingDBHelper.getInstance(this);
        mDBHelper.openReadLink();
        mDBHelper.openWriteLink();
        showGoods();
    }


    private void showGoods()
    {
        //商品条目是一个线性布局，设置布局宽度为屏幕的一半
        int screenWidth=getResources().getDisplayMetrics().widthPixels;;
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(screenWidth/2, ViewGroup.LayoutParams.WRAP_CONTENT);

        //查询商品数据库中的所有商品记录
        List<GoodsInfo> list=mDBHelper.queryAllGoodsInfo();
        for (GoodsInfo info : list) {
            //获取布局文件item_goods.xml的根视图
            View view= LayoutInflater.from(this).inflate(R.layout.item_goods,null);
            ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_price = view.findViewById(R.id.tv_price);
            Button btn_add = view.findViewById(R.id.btn_add);
            //给控价设置值
            iv_thumb.setImageURI(Uri.parse(info.picPath));
            tv_name.setText(info.name);
            tv_price.setText(String.valueOf(info.price));
            btn_add.setOnClickListener(v->{
                addToCart(info.id,info.name);
            });
            //将商品视图添加到网格布局
            gl_channel.addView(view,params);
            //添加点击事件，点击视图直接跳转到对应的商品详情页
            view.setOnClickListener(v->{
                Intent intent=new Intent(ShoppingMallActivity.this,ShoppingDetailActivity.class);
                intent.putExtra("goodsId",info.id);
                startActivity(intent);
            });
        }
    }
    //从购物车页面跳转回来商品选购页面时，可能会需要更新购物车数量，而此时执行的方法是OnResume，所以写在这里比较合适
    @Override
    protected void onResume() {
        super.onResume();
        showCartInfoTotal();
    }

    private void showCartInfoTotal() {
        int count = mDBHelper.countCartInfo();
        MyApplication.getInstance().goodsCount=count;
        tv_count.setText(String.valueOf(count));
    }

    private void addToCart(int goodsId, String name) {
        mDBHelper.insertCartInfo(goodsId);
        int count=++MyApplication.getInstance().goodsCount;
        tv_count.setText(String.valueOf(count));
        Toast.makeText(this, "已添加一部"+name+"到购物车", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDBHelper.closeLink();
    }
}
